<?php
$sub_menu = "230150";
include_once("./_common.php");

auth_check($auth[$sub_menu], "r");

$colspan = 8;

$g4['title'] = "등급설정";
include_once($g4['admin_path']."/admin.head.php");

$is_coupon = false;
if (file_exists($g4['path']."/plugin/coupon/_config.php"))
    $is_coupon = true;

$gd_id = null;

if ($sfl == "mb_id" && $stx) {
    $row = mw_cash_grade($stx);
    if ($row) {
        $gd_id = $row['gd_id'];
    }
    else
        $gd_id = -1;
}
?>

<form name=fsearch method=get>
<table width=100%>
<tr>
    <td width=50% align=left>
        <a href="<?php echo $_SERVER['PHP_SELF']?>"><?php echo subtitle($g4['title']); ?></a>
    </td>
    <td width=50% align=right>
        <select name=sfl class=cssfl>
            <option value='mb_id'>회원아이디</option>
        </select>
        <input type=text name=stx class=ed required itemname='검색어' value='<?=$stx?>'>
        <input type=image src='<?=$g4[admin_path]?>/img/btn_search.gif' align=absmiddle>
    </td>
</tr>
</table>
</form>

<table width=100% cellpadding=0 cellspacing=1>
<colgroup width=''>
<colgroup width=60>
<colgroup width=100>
<colgroup width=100>
<colgroup width=100>
<colgroup width=100>
<?php if ($is_coupon) echo "<colgroup width=70>"; ?>
<colgroup width=80>
<tr><td colspan='<?php echo $colspan?>' class='line1'></td></tr>
<tr class='bgcol1 bold col1 ht center'>
    <td> 등급 </td>
    <td> 사용 </td>
    <td> 기간 </td>
    <td> <?php echo $mw_cash['cf_cash_name']?> </td>
    <td> 추가적립 </td>
    <td> 인원 </td>
    <?php if ($is_coupon) echo "<td>쿠폰</td>"; ?>
    <td> <a href="mw.grade.form.php"><img src="<?php echo $g4['admin_path']?>/img/icon_insert.gif" border="0"></a> </td>
</tr>
<tr><td colspan='<?php echo $colspan?>' class='line2'></td></tr>

<?php
$t_acc = $t_now = 0;
$old = null;

$i=0;
$sql = "select * from {$mw_cash['grade_table']} order by gd_cash desc, gd_period*-1 desc, gd_point_save desc";
$qry = sql_query($sql);
while ($row=sql_fetch_array($qry))
{
    $sql = "select count(*) as cnt from ( ";
    $sql.= "select sum(cl_cash) as gd_cash ";
    $sql.= "  from {$mw_cash['cash_list_table']} ";
    $sql.= " where cl_status = '1' ";
    $sql.= "   and (cl_paytype <> '' and cl_paytype <> '서비스' and cl_paytype <> '포인트')";
    $sql.= "   and cl_cash > 0 and cl_payment > 0";
    if ($row['gd_period']) {
        $begin = date("Y-m-d 00:00:00", strtotime("-{$row['gd_period']} day", $g4['server_time']));
        $limit = date("Y-m-d 23:59:59", $g4['server_time']);
        $sql.= " and cl_datetime between '{$begin}' and '{$limit}' ";
    }
    $sql.= " group by mb_id ";
    if ($old['gd_cash'])
        $sql.= " ) as r where r.gd_cash >= '{$row['gd_cash']}' and r.gd_cash < '{$old['gd_cash']}'  ";
    else
        $sql.= " ) as r where r.gd_cash >= '{$row['gd_cash']}'  ";
    $row2 = sql_fetch($sql);

    if ($row['gd_period']) {
        $gd_period = $row['gd_period'].'일';
        $fr_date = date("Y-m-d", strtotime("-{$row['gd_period']} day", $g4['server_time']));
        $to_date = date("Y-m-d", $g4['server_time']);
    }
    else {
        $gd_period = "전체";
        $fr_date = "2000-01-01";
        $to_date = date("Y-m-d", $g4['server_time']);
    }
    
    if ($row['gd_use']) {
        $fr_cash = round($row['gd_cash']);
        $to_cash = round($old['gd_cash']);
        $old = $row;

        $t_acc += $row2['cnt'];
    }
    else {
        $fr_cash = round($row['gd_cash']);
        $to_cash = '';
    }

    if ($gd_id && $gd_id != $row['gd_id']) continue;

    $gd_point_save = "-";
    if ($row['gd_point_save'])
        $gd_point_save = $row['gd_point_save'] . "%";

    $list = $i%2;
?>

<tr class='list<?php echo $list?> col1 ht center'>
    <td> <?php echo $row['gd_name']?> </td>
    <td> <?php if ($row['gd_use']) echo "√"; ?> </td>
    <td> <?php echo $gd_period?> </td>
    <td> <?php echo number_format($row['gd_cash']).$mw_cash['cf_cash_unit']?> </td>
    <td> <?php echo $gd_point_save?> </td>
    <td> <a href="javascript:grade_log(<?php echo "'{$fr_date}', '{$to_date}', '{$fr_cash}', '{$to_cash}'"?>)"><?php echo number_format($row2['cnt'])?> 명</a> </td>
    <?php if ($is_coupon) echo "<td><input type='button' class='btn1' value='쿠폰' onclick='give_coupon({$row['gd_id']})'></td>"; ?>
    <td>
        <a href="mw.grade.form.php?gd_id=<?php echo $row['gd_id']?>&w=u"><img src="<?php echo $g4['admin_path']?>/img/icon_modify.gif" border="0"></a>
        <a href="javascript:del('mw.grade.update.php?w=d&gd_id=<?php echo $row['gd_id']?>');"><img src="<?php echo $g4['admin_path']?>/img/icon_delete.gif" border="0"></a>
    </td>
</tr>

<?php $i++; } ?>

<?php
if (!$i)
    echo "<tr> <td style='height:100px; text-align:center;' colspan='{$colspan}' class='b'> 자료가 없습니다. </td></tr>";
?>

<tr><td colspan='<?php echo $colspan?>' class='line2'></td></tr>
<tr class='bgcol1 bold col1 ht center'>
    <td style="font-weight:bold;"> 합 계 </td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><?php echo number_format($t_acc)?> 명</td>
    <td> &nbsp; </td>
    <td> &nbsp; </td>
</tr>
<tr><td colspan='<?php echo $colspan?>' class='line1'></td></tr>
</table>

<script>
function grade_log(fr_date, to_date, fr_cash, to_cash) {
    location.href = "mw.cash.statistics6.php?ymd=d&fr_date=" + fr_date + "&to_date=" + to_date + "&fr_cash=" + fr_cash + "&to_cash=" + to_cash;
}

function give_coupon(gd_id) {
    location.href = "mw.grade.coupon.php?gd_id=" + gd_id + "&mb_id=<?php echo $stx?>";
}
</script>

<?php

include_once ($g4['admin_path']."/admin.tail.php");
